'use strict';

angular.module('fbdApp.uploadModule')

.controller('fileCtrl', function($scope, Upload) {
  // $scope.$parent.uploadSuccess = false;
  // console.log($scope.newTemp);
  // console.log($scope.fbdFile);
  $scope.uploadFile = function(file) {
    // console.log($parent.newTemp);
    // console.log($scope.fbdFile);
    var req = {
      url: 'http://localhost:3000/api/upload',
      data: {
        file: file
      }
    };
    // console.log(file.name);
    // shareData.realFileName = file.name;
    $scope.realFileName = file.name;
    Upload.upload(req).then(function(res) {
      $scope.isUploaded = true;
      $scope.uploadInfo = '上传成功！';
      $scope.fileName = res.data;
      $scope.$emit('isUploaded', $scope.isUploaded);
      // shareData.fileName = $scope.fileName;

      // $scope.$emit('fileName', $scope.fileName);

    }, function(err) {
      $scope.isUploaded = false;
      $scope.uploadInfo = '上传失败！原因：' + err.data;

      $scope.$emit('isUploaded', $scope.isUploaded);
      // console.log(err);
      // alert(err);
      // console.log(err);
    });
  }
  $scope.$watch('fbdFile', function (newval, oldval) {
    if (newval) {
      $scope.uploadFile(newval);
    }
    
  })

});
